package com.jhgsys.internal.accident.service.impl;

import com.jhgsys.internal.accident.entity.AccidentReportReason;
import com.jhgsys.internal.accident.entity.AccidentReportReasonFile;
import com.jhgsys.internal.accident.mapper.AccidentReportReasonFileMapper;
import com.jhgsys.internal.accident.service.IAccidentReportReasonFileService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *  Service实现
 *
 * @author zhangwei
 * @date 2024-08-26 09:36:00
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AccidentReportReasonFileServiceImpl extends ServiceImpl<AccidentReportReasonFileMapper, AccidentReportReasonFile> implements IAccidentReportReasonFileService {

    /**
     * 查询（分页）
     * @author zhangwei
     * @date 2024-08-26 09:36:00
     * @param request QueryRequest
     * @param accidentReportReasonFile accidentReportReasonFile
     * @return IPage<AccidentReportReasonFile>
    */
    @Override
    public IPage<AccidentReportReasonFile> findAccidentReportReasonFilePage(QueryRequest request, AccidentReportReasonFile accidentReportReasonFile) {
        LambdaQueryWrapper<AccidentReportReasonFile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccidentReportReasonFile::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<AccidentReportReasonFile> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author zhangwei
     * @date 2024-08-26 09:36:00
     * @param accidentReportReasonFile accidentReportReasonFile
     * @return List<AccidentReportReasonFile>
     */
    @Override
    public List<AccidentReportReasonFile> findAccidentReportReasonFileList(AccidentReportReasonFile accidentReportReasonFile) {
	    LambdaQueryWrapper<AccidentReportReasonFile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccidentReportReasonFile::getDeletedFlg, 0);
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author zhangwei
    * @date 2024-08-26 09:36:00
    * @param id
    * @return AccidentReportReasonFile
    */
    @Override
    public AccidentReportReasonFile findAccidentReportReasonFileById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author zhangwei
     * @date 2024-08-26 09:36:00
     * @param accidentReportReasonFile accidentReportReasonFile
     */
    @Override
    @Transactional
    public void createAccidentReportReasonFile(AccidentReportReasonFile accidentReportReasonFile) {
        accidentReportReasonFile.setCreatedTime(new Date());
        this.save(accidentReportReasonFile);
    }

    /**
     * 修改
     * @author zhangwei
     * @date 2024-08-26 09:36:00
     * @param accidentReportReasonFile accidentReportReasonFile
     */
    @Override
    @Transactional
    public void updateAccidentReportReasonFile(AccidentReportReasonFile accidentReportReasonFile) {
        accidentReportReasonFile.setUpdatedTime(new Date());
        this.saveOrUpdate(accidentReportReasonFile);
    }

    /**
     * 逻辑删除
     * @author zhangwei
     * @date 2024-08-26 09:36:00
     * @param accidentReportReasonFile accidentReportReasonFile
     * @param accidentReportReasonFileIds accidentReportReasonFileIds
     */
    @Override
    @Transactional
    public void deleteAccidentReportReasonFile(AccidentReportReasonFile accidentReportReasonFile,String accidentReportReasonFileIds) {
        List<String> list = Arrays.asList(accidentReportReasonFileIds.split(StringPool.COMMA));
        this.baseMapper.update(accidentReportReasonFile,new LambdaQueryWrapper<AccidentReportReasonFile>().in(AccidentReportReasonFile::getReportReasonFileId,list));
	}

    /**
    * 物理删除
    * @author zhangwei
    * @date 2024-08-26 09:36:00
    * @param accidentReportReasonFileIds accidentReportReasonFileIds
    */
    @Override
    @Transactional
    public void physicallyDeleteAccidentReportReasonFile(String accidentReportReasonFileIds) {
        List<String> list = Arrays.asList(accidentReportReasonFileIds.split(StringPool.COMMA));
        LambdaQueryWrapper<AccidentReportReasonFile> wapper = new LambdaQueryWrapper<>();
        wapper.in(AccidentReportReasonFile::getReportReasonFileId,list);
        this.remove(wapper);
    }

    /**
     * 根据事故原因id删除
     * @author admin
     * @date 2024-08-27 10:56:15
     * @param reportIds
     */
    @Override
    @Transactional
    public void deleteByReportReasonIds(String reportReasonIds,String fileType) {
        List<String> list = Arrays.asList(reportReasonIds.split(StringPool.COMMA));
        LambdaQueryWrapper<AccidentReportReasonFile> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(AccidentReportReasonFile::getReportReasonId,list);
        wrapper.eq(AccidentReportReasonFile::getReportFileType,fileType);
        this.remove(wrapper);
    }
}
